package com.bite;

import java.sql.Array;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Test1 {
    //遍历数组
    public static void main(String[] args) {
        int[] array= {1,2,3,4,5,6};
        byte[] array2= {1,2,3,5,5,};
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
        //for-each循环
        System.out.println("=========");
        for (int x:
             array) {
            System.out.print(x + " ");
        }
        System.out.println();
        System.out.println("=========");
        //使用Arrays的toString方法,把传入的参数array,表示数组,以字符串的方式输出
        String ret = Arrays.toString(array2);
        System.out.println(ret);
    }
    public static void main8(String[] args) {
        int[] array = {1,2,3,4,5};
        System.out.println(array[15]);//会越界访问报错.ArrayIndexOutOfBoundsException
    }

    public static void main7(String[] args) {
        //数组对象
        int[] array = {1,2,3,4,5,5};
        int[] array1 = new int[]{1,2,3,4,5,5,6};
        int[] array2 = new int[10];//没有初始化当前指定的值    默认为0
        boolean[] array5 = new boolean[3]; //boolean类型的默认值为false
//    int[] array4;
//    array4 = {1,23,4,5,6};//初始化要和定义一起,会报错
//    int array3[] = {1,2,3,4,5};//不推荐使用,
        //计算组成他们的数字之和
    }

    public static int ret(int n) {
        if(n < 10) {
            return n;
        }
        return (n%10) + ret(n/10);
    }

    public static void main6(String[] args) {
        int i = ret(1729);
        System.out.println(i);
    }




    //计算计算递推1 + 2 + 3 + 4....
    public static int sum(int n) {
      if(n == 0) {
          return 0;
      }
      return n + sum(n-1);
    }

    //打印每一位数
    public static void print(int num) {
        if(num < 10) {
            System.out.println(num % 10);
            return;
        }
        print(num/10);
        System.out.println(num % 10);
    }


    public static void main5(String[] args) {
        System.out.println(sum(10));
        print(123);
    }
    //方法重载与返回类型无关
    //方法名一样
    //参数(类型,个数,顺序)不一样
    public static int add(int a, int b) {
        return a + b;
    }
//    public static double add(int a, int b) {
//        return a + b;
//    }
    public static int add(int a, int b,int c) {
        return a + b;
    }

    public static void main4(String[] args) {
        add(1,2);
    }










    public static  boolean isLeapYear(int year) {
        if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            return true;
        }else {
            return false;
        }

    }

    public static void main3(String[] args) {
        int year = 1000;
        boolean ret = isLeapYear(year);
        //&year;//java当中拿不到栈上的地址对的
        if (ret == true) {
            System.out.println("闰年");
        }else {
            System.out.println("不是闰年");
        }
    }
    /*
    public static 返回值 方法名(形式参数列表) {
            方法体
    }

     */


    public static void main2(String[] args) {
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);
        int randNum = random.nextInt(100);//生成一个随机数[0,100)
        System.out.println(randNum);
        System.out.print("请输入0-100之间的数字: ");
        int i = scanner.nextInt();
        while(true) {
            if(randNum < i) {
                System.out.println("猜大了");
                i = scanner.nextInt();
            }else if(randNum > i) {
                System.out.println("猜小了");
                i = scanner.nextInt();
            }else {
                System.out.println("猜对了");
                break;
            }
        }
    }
    public static void main1(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个整数: ");
        int i = sc.nextInt();
        System.out.println(i);
        System.out.println("输入一个浮点数");
        float f = sc.nextFloat();
        System.out.println(f);
        sc.nextLine();
        //注意在这里会识别上一个输入结尾的回车
        System.out.println("输入一个字符串");
        String str = sc.nextLine();
        System.out.println(str);
    }
}
